#!/bin/bash
#
# Install mysql from yum and sets up the mysql home and permissions
#

echo "================================================================="
echo "Install mysql ($SERVER_NAME): begin"

MYSQL_HOME=/db/mysql

source $COMMON_FUNC > /dev/null 2>&1
if [ $? -gt 0 ]; then
    echo "Error: This script requires common-func.bash"
    exit 1
fi

isRoot

################################################################################
# yum install
echo "Installing mysql software from yum repository"
yum install -y mysql  mysql-server

################################################################################
# mysql user
user_exists mysql
if [ $? -eq 1 ]; then
    echo "Creating user mysql"
    useradd -r mysql
else
    echo "User mysql already exists, nothing to do"
fi

echo "Adding user mysql to dbdat group"
usermod -a -G dbdat mysql

################################################################################
# mysql home
echo "Creating home directory for mysql database: $MYSQL_HOME"
mkdir -p $MYSQL_HOME

echo "Creating root link (/msyql) to $MYSQL_HOME"
ln -s $MYSQL_HOME /mysql

echo "Applying permissions for msyql"
chown -R mysql:mysql $MYSQL_HOME
chmod -R 775 $MYSQL_HOME
chown mysql:mysql /mysql
chmod 770 /mysql

################################################################################
# SELinux

#echo "Temporarily shutting off SELinux"
#setenforce 0

#echo "Modifying SELinux properties for /db"
#chcon -t var_lib_t /db

#echo "Modifying SELinux properties for $MYSQL_HOME"
#chcon -R -u system_u -r object_r -t mysqld_db_t $MYSQL_HOME

echo "Modifying SELinux properties for /mysql"
chcon -R -u system_u -r object_r -t mysqld_db_t /mysql

################################################################################
# installing mysql configuration file
echo "Backing up /etc/my.cnf"
backup /etc/my.cnf
if [ ! $? -eq 0 ];
then
    echo "Error: Encountered error during backup of /etc/my.cnf."
	pause '> Press [ENTER] to continue or [CTRL-C] to exit'
fi

echo "Installing new /etc/my.cnf"
rm -Rf /etc/my.cnf
cp $RESOURCES_DIR/mysql/my.cnf /etc/my.cnf

echo "Removing any default mysql items at /var/lib/mysql"
rm -Rf /var/lib/mysql/*

################################################################################
# restarting mysql service
echo "Restarting the mysqld service"
service mysqld start
if [ ! $? -eq 0 ];
then
    echo "Error: Encountered error while starting mysql server"
    exit 1
fi

sleep 5

echo "Creating soft link to the new location of mysql.sock"
ln -s /mysql/mysql.sock /var/lib/mysql/mysql.sock

################################################################################
# open mysql port
echo "Creating iptables for mysql (port 3306)"
open_port 3306
if [ ! $? -eq 0 ];
then
    echo "Encountered error during open_port 3306"
    pause '> Press [ENTER] to continue or [CTRL-C] to exit'
fi

echo "Install mysql: done"
echo "Please run mysql_secure_installation"
echo "================================================================="